import uuid

from django.db import models

# Create your models here.
class ProductCategory(models.Model):
    name = models.CharField(max_length=50, primary_key=True, verbose_name="")
    title = models.CharField(max_length=50)
    description = models.TextField(default="")
    image = models.ImageField(null=True)
    parent = models.ForeignKey('ProductCategory', null=True, on_delete=models.SET_NULL)
    # time = models.DateTimeField()
    # def to_json(self):
    #     return {
    #         "name": self.name,
    #         "title": self.title,
    #         "description": self.description,
    #         "image": self.image.path if self.image else "",
    #         "parent": self.parent.pk if self.parent else "",
    #         "datetime": self.time.strftime("%Y%m%d %H%M%S")
    #     }


class Product(models.Model):
    name = models.CharField(primary_key=True, max_length=50)
    title = models.CharField(max_length=50)
    image = models.ImageField(null=True)
    category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE)

class Order(models.Model):
    no = models.CharField(primary_key=True, max_length=100)
    products = models.ManyToManyField(Product)


class User(models.Model):
    telephone = models.CharField(max_length=20, primary_key=True)
    icon = models.ImageField(null=True)
    nick_name = models.CharField(max_length=20)
    password = models.CharField(max_length=100)
    salt = models.CharField(max_length=100)


# order = Order(no=uuid.uuid4().hex)
# p1 = Product.objects.get(name='a')
# p2 = Product.objects.get(name='b')
# p3 = Product.objects.get(name='b')
#
# order.products.add(p1)
# order.products.add(p2)
# order.products.add(p3)
#
# order.save()